package com.dreamoo.common.threadpool;

import com.dreamoo.common.threadpool.wrapper.DreamooTaskWrapper;
import lombok.Data;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author 山顶洞人郑某
 * @className DreamooThreadPoolExecutor.java 装饰的线程池 支持ThreadLocal 的传递
 * @date 2025 05 06
 */

public class DreamooThreadPoolExecutor {
    /**
     * 原本的线程池
     */
    private final ThreadPoolExecutor executor;

    public DreamooThreadPoolExecutor(ThreadPoolExecutor executor) {
        this.executor = executor;
    }

    /**
     * 修饰的线程池任务执行
     * @param runnable 任务 无返回值
     */
    public void execute(Runnable runnable) {
        executor.execute(DreamooTaskWrapper.wrap(runnable));
    }

    /**
     * 修饰的线程池任务执行
     * @param callable 任务 有返回值
     * @return Future<T> 任务执行结果
     * @param <T> 任务执行结果
     */
    public <T> Future<T> submit(Callable<T> callable) {
       return executor.submit(DreamooTaskWrapper.wrap(callable));
    }
}
